<?php
function pre_echo ($param) {
	if (is_array($param)) {
		echo '<pre>'.print_r($param, true).'</pre>'."\r";
	} else {
		echo '<pre>'.$param.'</pre>'."\r";
	}
}
function loadMod ($mod_info, $param='', $uid='') {
	$uid			= intval($uid);
	if (!$uid)		$uid = intval ($_SESSION['user_id']);
	$explode		= explode('/', $mod_info);
	$mod_path 	= (preg_match( '/^[_a-zA-Z0-9-]+$/', $explode[0])) ? $explode[0] : false;
	$mod_name 	= (preg_match( '/^[_a-zA-Z0-9-]+$/', $explode[1])) ? $explode[1] : false;
	if (preg_match('/^(\d+)(Y|M|W|D|H|I|S{1})$/i', $explode[2])) {
		switch (strtoupper(substr($explode[2], -1))) {
			case 'Y':	$cache_time = 60 * 60 * 24 * 365 * substr($explode[2], 0, -1);		break;
			case 'M':	$cache_time = 60 * 60 * 24 * 30 * substr($explode[2], 0, -1);		break;
			case 'W':	$cache_time = 60 * 60 * 24 * 7 * substr($explode[2], 0, -1);		break;
			case 'D':	$cache_time = 60 * 60 * 24 * substr($explode[2], 0, -1);		break;
			case 'H':	$cache_time = 60 * 60 * substr($explode[2], 0, -1);			break;
			case 'I':	$cache_time = 60 * substr($explode[2], 0, -1);				break;
			case 'S':	$cache_time = intval(substr($explode[2], 0, -1));				break;
			default :	$cache_time = 0;									break;
		}
	} else $cache_time = intval($explode[2]);
        
        // ###################################################
        $cache_time = 0;
        // ###################################################

	if ($mod_path and $mod_name and defined('PATH_SMARTY_TEMPLATE') and defined('PATH_SMARTY_COMPILE') and defined('PATH_SMARTY_CACHE')) {
		// 每个用户设置不同的缓存路径
		$format = sprintf('%012s', $uid);		// 32 bit systems have a maximum signed integer range of -2147483648 to 2147483647
		$a = substr($format, 0, 3) . '/';
		$b = substr($format, 3, 3) . '/';
		$c = substr($format, 6, 3) . '/';
		$d = substr($format, 9, 3) . '/';
		// 检查路径是否存在，如果不存在，则创建
		if (!file_exists(PATH_SMARTY_COMPILE . $a . $b . $c . $d)) {
			createPath(PATH_SMARTY_COMPILE . $a);
			createPath(PATH_SMARTY_COMPILE . $a . $b);
			createPath(PATH_SMARTY_COMPILE . $a . $b . $c);
			createPath(PATH_SMARTY_COMPILE . $a . $b . $c . $d);
		}
		if (!file_exists(PATH_SMARTY_CACHE . $a . $b . $c . $d)) {
			createPath(PATH_SMARTY_CACHE . $a);
			createPath(PATH_SMARTY_CACHE . $a . $b);
			createPath(PATH_SMARTY_CACHE . $a . $b . $c);
			createPath(PATH_SMARTY_CACHE . $a . $b . $c . $d);
		}

		$php_name		= realpath(PATH_SMARTY_TEMPLATE . $mod_path . '/' . $mod_name . '.php');		// 生成数据的php文件名
		$tpl_name		= realpath(PATH_SMARTY_TEMPLATE . $mod_path . '/' . $mod_name . '.tpl');		// 绝对路径的tpl名称
		$class_name 	= 'ui_' . $mod_path . '_' . $mod_name;
		if (file_exists($php_name) and file_exists($tpl_name)) {
			$S = new Smarty();
			$S->setTemplateDir(PATH_SMARTY_TEMPLATE);
			$S->setCompileDir(PATH_SMARTY_COMPILE . $a . $b . $c . $d);
			$S->setCacheDir(PATH_SMARTY_CACHE . $a . $b . $c . $d);
			$S->setCompileCheck(Smarty::COMPILECHECK_CACHEMISS);
//			$S->force_compile = true;
			if ($cache_time > 0) {
				$S->setCaching(Smarty::CACHING_LIFETIME_CURRENT);	
				$S->setCacheLifetime($cache_time);
			} elseif ($cache_time == 0) {
				$S->clearCache($tpl_name, $uid);
				$S->setCaching(0);
				$S->setCacheLifetime(0);
			} else {
				$S->clearCache($tpl_name, $uid);
				return false;
			}
			include($php_name);							// 引入php文件
			new $class_name($S, $param);					// 执行class并把Smarty实例和参数带进去，assign数据
			$return = $S->fetch($tpl_name, $uid);				// smarty模板的返回结果
			$S->clearAllAssign();							// 清空assign的数据
			return $return ;
		} else return false;
	} else return false;
	
}
function makeURL ($input) {
	parse_str($input, $inputArray);						// 把输入的字符串转换成数组
	if (is_array($inputArray) and count($inputArray)>0) {
		if ($inputArray['clear']) {						// 如果有clear，则只保留input中的数据
			unset($inputArray['clear']);
			$newArray = $inputArray;
		} else {										// 没有clear，合并input和GET，用input中的数据覆盖GET中的数据
			$newArray = array_merge($_GET, $inputArray);
		}
	} else {											// 没有input或者input格式错误，则保留原GET
		$newArray = $_GET;
	}
	// 去掉newArray中的空白值
	if (is_array($newArray)) {
		foreach ($newArray as $k=>$v) {
			if (empty($v)) {
				unset($newArray[$k]);
			}
		}
	}
	// 构造返回的URL
	if (is_array($newArray)) {
		return $_SERVER['PHP_SELF'].'?'.http_build_query($newArray);
	} else {
		return $_SERVER['PHP_SELF'];
	}
}


function die2reload ($str='') {
	echo '
		<script language="javascript">
			location.href="'.makeURL($str).'";
		</script>
	';
	die();
}
function die2page ($str='') {
	echo '
		<script language="javascript">
			location.href="'.$str.'";
		</script>
	';
	die();
}
/**
 * 创建路径
 *
 * @param String $pathName				// 路径名称
 * @param String $mod				// 文件夹权限，默认为0777
 */
function createPath ($pathName, $mod=0777) {
	if (!file_exists($pathName)) {
		mkdir($pathName, $mod);
		chmod($pathName, $mod);
	}
}
/**
 * 复制文件
 *
 * @param String $in				// 源文件
 * @param String $out				// 目标文件
 * @param String $mod				// 目标文件权限，默认为0666
 */
function copyFile ($in, $out, $mod=0666) {
	copy($in, $out);
	chmod($out, $mod);
}
/**
 * 移动文件
 *
 * @param String $in				// 源文件
 * @param String $out				// 目标文件
 * @param String $mod				// 目标文件权限，默认为0666
 */
function moveFile ($in, $out, $mod=0666) {
	rename($in, $out);
	chmod($out, $mod);
}
/**
 * 计算时间与现在时间的差距
 *
 * @param int $time
 * return string
 */
function time2myformat($time) {
	$sec_diff = time() - intval($time);   									//得到时间差
	if ($sec_diff < 60) {								// 1分钟之内
		$return = $sec_diff.'秒前';
	} elseif ($sec_diff < 60*60) {						// 1小时之内
		$return = floor($sec_diff/60).'分钟前';
	} elseif ($sec_diff < 60*60*24) {					// 1天之内
		//$return = floor($sec_diff/3600).'小时'. floor($sec_diff%3600/60).'分钟前';
		$return = floor($sec_diff/3600).'小时前';
	} else {
		$return = date('m月d日', $time);
	}
	return $return;
}
/**
 * 把url字符串中#及其后面的部分去掉
 * 如果url是以“/”结尾，则去掉“/”
 *
 * @param String $url
 * @return String
 */
function url_cut_tail ($url) {
	if ($url) {
		$explode = explode('#', $url);
		$url = $explode[0];
		if (substr($url, -1) == '/') {
			$url = substr($url, 0, -1);
		}
		return $url;
	} else return false;
}
function microtime_float () {
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}
function format_id_string ($str, $return_array=true) {
	if (is_string($str) and !empty($str)) {
		$explode = explode(',', $str);
		$tmp = array();
		foreach ($explode as $v) {
			$v = trim($v);
			if (preg_match('/^\d+$/', $v)) {
				$tmp[] = $v;
			}
		}
		if (!empty($tmp)) {
			if ($return_array) {
				return $tmp;
			} else {
				return implode(',', $tmp);
			}
		} else return false;
	} else return false;
}

function format_nickname ($nickname) {
	return addslashes(strip_tags(trim($nickname)));
}

function is_weibo_id ($weibo_id) {
	return preg_match('/^\d+$/', $weibo_id);
}

function is_renren_id ($renren_id) {
	return preg_match('/^\d+$/', $renren_id);
}

function is_kaixin001_id ($kaixin001_id) {
	return preg_match('/^\d+$/', $kaixin001_id);
}

function is_qqwb_id ($qqwb_id) {
	return preg_match('/^[a-z0-9]+$/i', $qqwb_id);
}
?>